<?php

if(PARCL_EXEC !== 1) {
    die('<h1>Incorrect Access</h1>You cannot access this file directly.<br/>' .
         'If you got this error message by clicking a link on the site,' .
         'please notify the webmaster as soon as possible.');
}

/**
 * Purpose     : To wrap the construction the the standard navigation menu.
 * Description : This class does some of the functions that the prospective links
                 class is supposed to do in the future, like preparing links to
                 proper links, etc.
 * - Construct -
 * Input       : $active: The active page as a string value.
 * Output      : Not applicable.
 * Description : Just binds the function arguments to a class variable.
 * Errors      : No error conditions have been specified yet.
 * Assumptions : That $active is a string.
 * Dev Notes   : The fact that (string) is enforced as datatype, reduces the risk
                 of harmful things making its way through that variable.
 */

class Menu {
    protected $types = array();
    protected $parentId = array();
    protected $links;
    protected $isnavi = null;
    protected $id = null;
    protected $active = null;
    public function __construct() {
        $registry = Registry::GetInstance();
        $this->active = (string) $registry->Current->page;
        $this->types = array( 'internal' => 'index.php?' . $registry->conf->pageSwitch . '=%s',
                               'home' => null);
    }

    /**
     * Input       : None.
     * Output      : $links: A properly formed array for template parsing.
     * Description : This function does everything necessary to properly generate
                     the menu and insert it into an array ready for template parsing.
     * Errors      : Throws an error if the 'type' field in the links table contains
                     a non-supported type.
     * Assumptions : That the Registry class is available and that GetMenu() does
                     what it's supposed to.
     * Dev Notes   : The link check function is supposed to be in the upcoming links
                     class.
     */

    public function Execute() {
        $registry = Registry::GetInstance();
        $isnavi = 'navi';
        $this->links = MenuQueries::GetMenu(($isnavi == 'navi' ? 1 : 0), $registry->Current->pageID);
        
        foreach($this->links as $k => &$l) {
            if(!array_key_exists($l['type'], $this->types)) {
                throw new GeneralException('Non-supported link in database.',
                                        GeneralException::NOSUPPORT);
            }
            $l['active'] = (($l['link'] == 'home' ? $registry->conf->startPage : $l['link'])  == $this->active ? 1 : 0);
            $l['link'] = call_user_func(array($this, $l['type'] . 'Link'), $l['link']);
            if(is_null($l['description'])) {
                $l['description'] = 0;
            }
        }
        return $this->links;
    }

    /**
     * Input       : $isnavi: String containing either 'navi' or not.
                     $id: The id of the current page.
     * Output      : None.
     * Description : Binds the variables to class variables. These two variables
                     are the values of the markers in the prepared statement.
     * Errors      : None.
     * Assumptions : That $id is of correct value.
     * Dev Notes   : Not sure if this is the right approach.
     */

    private function setMenu($isnavi, $id) {
        $this->isnavi = ($isnavi == 'navi' ? 1 : 0);
        $this->id = $id;
    }

    /**
     * Input       : $link: The 'link' field of the links table, by reference.
     * Output      : The complete link ready to be inserted in an <a href=...
     * Description : A simple link definition application function. Just applies
                     the link type specified directly into the output array.
     * Errors      : No error conditions have been defined yet.
     * Assumptions : That $link is of correct type.
     * Dev Notes   : This is one of the link class functions.
     */

    protected function internalLink(&$link) {
        $structure = $this->types['internal'];
        return sprintf($structure, $link);
    }

    /**
     * Input       : $link: The 'link' field of the links table, by reference.
     * Output      : The complete link ready to be inserted in an <a href=...
     * Description : A simple link definition application function. Just applies
                     the link type specified directly into the output array.
     * Errors      : No error conditions have been defined yet.
     * Assumptions : That $link is of correct type and that the Registry class is available.
     * Dev Notes   : This is one of the link class functions.
     */

    protected function homeLink(&$link) {
        $registry = Registry::GetInstance();
        $structure = $this->types['internal'];
        return sprintf($structure, $registry->conf->startPage);
    }
}